package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import static java.lang.Class.forName;

public class JDBCDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        /*
           大致步骤:
           1:加载驱动
           2:通过DriverManager与数据库建立链接
           3:通过链接对象Connection创建语句对象Statement
           4:通过Statement对象执行SOL语句
           5:得到结果(如果执行的是DQL，则会得到查询结果集ResultSet)
           6:处理结果集
           7:关闭连接
         */
        //1:加载驱动
        // 加载MysqL驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2:通过DriverManager与数据库建立链接
        Connection connection = DriverManager.getConnection(
                //                           /数据库名？...
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root");
        System.out.println("与数据库连接成功！");

        //3:通过链接对象Connection创建语句对象Statement
        Statement statement = connection.createStatement();

        String sql = "CREATE TABLE userinfo(\n" +
                "    id INT PRIMARY KEY AUTO_INCREMENT,\n" +
                "    username VARCHAR(30),\n" +
                "    password VARCHAR(30),\n" +
                "    nickname VARCHAR(30),\n" +
                "    age INT\n" +
                ")";
        //4:通过statement对象执行SQL语句
        statement.execute(sql);
        System.out.println("创建表成功！");
        connection.close();
    }
}
